package com.fenghongzhang.shop1903;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.databinding.BindingAdapter;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.fenghongzhang.shop1903.databinding.RvLayoutBinding;

import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

    private List<User> list;

    public MyAdapter(List<User> list) {
        this.list = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_layout, null, false);
        ViewHolder viewHolder = new ViewHolder(inflate);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        User user = list.get(position);
//        holder.binding.setUser(user);
        holder.bind(user);
    }

    @BindingAdapter({"android:src"})
    public static void setImage(ImageView image,Integer id){
        image.setImageResource(id);
    }

    @BindingAdapter({"url"})
    public static void setImage(ImageView image,String url){
        Glide.with(image.getContext()).load(url).into(image);
    }



    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private RvLayoutBinding binding;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
             binding = DataBindingUtil.bind(itemView);
        }

        public void bind(User user){
            binding.setUser(user);
        }

    }
}
